package com.tmpt.controller.taglib;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.PageContext;

import org.apache.log4j.Logger;

import com.tmpt.dto.A1TemplateColumnDto;
import com.tmpt.dto.A1TemplateLineTailMenuDto;
import com.tmpt.dto.A1TemplateTableDto;
import com.tmpt.service.ITemplateService;

public class TemplateHistoryTag {

	private Logger log = Logger.getLogger(this.getClass());
	
	@Resource(name="pageContext") private PageContext pageContext;
	@Resource(name="templateService") private ITemplateService templateService;

	private A1TemplateTableDto getTemplateTable(String keyRandom){
		HttpSession session = pageContext.getSession();
		A1TemplateTableDto templateTable = (A1TemplateTableDto)session.getAttribute("templateTable");
		//if(templateTable == null){
			templateTable = templateService.getTemplateTable(keyRandom);
			//session.setAttribute("templateTable", templateTable);
		//}
		return templateTable;
	}
	
	public Map<String,Object> getColumnList(Map<String,String> paramMap){
		String keyRandom = paramMap.get("keyRandom");
		String _ishistory = paramMap.get("_ishistory");
		
		A1TemplateTableDto templateTable = getTemplateTable(keyRandom);
		List<A1TemplateColumnDto> columnList = templateTable.getA1TemplateColumnDtoList();

		List<A1TemplateLineTailMenuDto> lineTailMenuDtoList = templateTable.getA1TemplateLineTailMenuDtoList();
		System.out.println("------lineTailMenuDtoList:"+lineTailMenuDtoList);
		
		List<A1TemplateColumnDto> column4Add = new ArrayList<A1TemplateColumnDto>();//add and edit dialog
		StringBuilder sb = new StringBuilder("[");
		StringBuilder sbFrozenColumn = new StringBuilder("[");
		for(A1TemplateColumnDto a1TemplateColumnDto:columnList){
			String columnName = a1TemplateColumnDto.getColumnName();
			String columnShowName = a1TemplateColumnDto.getColumnShowName();
			int isFrozenColumn = a1TemplateColumnDto.getIsFrozenColumn();
			int isHidden = a1TemplateColumnDto.getIsHidden();
			int canAdd = a1TemplateColumnDto.getCanAdd();
			if(isHidden==1){ //可能是主键ID,页面不展示
				continue;
			}
			if(isFrozenColumn == 1){
				sbFrozenColumn.append("{field:'").append(columnName).append("',title:'").append(columnShowName).append("'},");
			}else{
				sb.append("{field:'").append(columnName).append("',title:'").append(columnShowName).append("'},");
			}
			if(canAdd == 1){
				column4Add.add(a1TemplateColumnDto);
			}
		}
		if(sb.length()>3)
			sb.delete(sb.length()-1,sb.length());
		if(sbFrozenColumn.length()>3)
			sbFrozenColumn.delete(sbFrozenColumn.length()-1,sbFrozenColumn.length());
		
		sb.append("]");
		sbFrozenColumn.append("]");
		
		String sbFrozenColumnStr = sbFrozenColumn.toString();
		String sbColumnStr = sb.toString();
		System.out.println("-----------"+sbColumnStr);
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("columns", sbColumnStr);
		map.put("frozenColumns", sbFrozenColumnStr);
		map.put("column4Add", column4Add);
		
		return map;
	}

}
